package com.nopay.cdxphone.ui.ativity;

import android.os.Bundle;

import com.nopay.cdxphone.base.BaseActivity;
import com.nopay.cdxphone.bean.response.GetRichTextResponse;
import com.ycbjie.webviewlib.inter.InterWebListener;
import com.ycbjie.webviewlib.utils.ToastUtils;
import com.ycbjie.webviewlib.utils.X5WebUtils;

import java.util.List;

import com.nopay.cdxphone.Constants;
import com.nopay.cdxphone.R;
import com.nopay.cdxphone.databinding.ActivityProblemAnswerBinding;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @ProjectName: myVisualPhone
 * @Package: cn.cn.myvisualphone.ui.ativity
 * @ClassName: ForgetActivity
 * @Description: 更新用户姓名
 * @Author: anqi
 * @CreateDate: 2021/5/23 8:05
 * @UpdateUser: 更新者
 * @UpdateDate: 2021/5/23 8:05
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class ProblemAnswerActivity extends BaseActivity<ActivityProblemAnswerBinding> {

    @Override
    protected void init(Bundle savedInstanceState, Object... preData) {
        binding.rlRoot.setPadding(0, getStatusBarHeight(ProblemAnswerActivity.this), 0, 0);
        binding.ivBack.setOnClickListener(v -> mSwipeBackHelper.backward());

        binding.progress.show();
        binding.progress.setColor(this.getResources().getColor(R.color.colorAccent),this.getResources().getColor(R.color.colorPrimaryDark));

        binding.webView.getX5WebChromeClient().setWebListener(interWebListener);
        binding.webView.getX5WebViewClient().setWebListener(interWebListener);
        if (!X5WebUtils.isConnected(this)){
            ToastUtils.showRoundRectToast("请先连接上网络");
        }
//        binding.webView.getSettingsExtension().setShouldRequestFavicon(false);

        api.doGetProblemAnswer().enqueue(new Callback<GetRichTextResponse>() {
            @Override
            public void onResponse(Call<GetRichTextResponse> call, Response<GetRichTextResponse> response) {
                if (response.body() != null) {
                    if (Constants.RESULT_OK.equals(response.body().getCode())) {
                        List<GetRichTextResponse.Data> dataList = response.body().getData();
                        if(dataList != null && dataList.size() > 0) {
                            String htmlStr = "<html><body>" + dataList.get(0).getContent() + "</body></html>";
                            binding.webView.loadDataWithBaseURL(null, htmlStr, "text/html", "UTF-8", null);
                        }
                    } else {
                        ToastUtils.showRoundRectToast(response.body().getMsg());
                    }
                }
            }

            @Override
            public void onFailure(Call<GetRichTextResponse> call, Throwable t) {
                ToastUtils.showRoundRectToast(getActivity().getResources().getString(R.string.call_api_error));
            }
        });
    }

    private InterWebListener interWebListener = new InterWebListener() {
        @Override
        public void hindProgressBar() {
            binding.progress.hide();
        }

        @Override
        public void showErrorView(@X5WebUtils.ErrorType int type) {
            switch (type){
                //没有网络
                case X5WebUtils.ErrorMode.NO_NET:
                    break;
                //404，网页无法打开
                case X5WebUtils.ErrorMode.STATE_404:

                    break;
                //onReceivedError，请求网络出现error
                case X5WebUtils.ErrorMode.RECEIVED_ERROR:

                    break;
                //在加载资源时通知主机应用程序发生SSL错误
                case X5WebUtils.ErrorMode.SSL_ERROR:

                    break;
                default:
                    break;
            }
        }

        @Override
        public void startProgress(int newProgress) {
            binding.progress.setWebProgress(newProgress);
        }

        @Override
        public void showTitle(String title) {

        }
    };

}
